﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using WiimoteLib;

namespace TayoukiNameSpace
{
    public partial class GameForm : Form
    {
        private Image avatarHappy;

        private Image avatarSad;

        private Image avatarCongrats;

        private Image waitLoading;

        public const String GOOD_JOB = "goodJob";
        public const String WRONG = "wrong";
        public const String FINISH = "finish";


        Wiimote wm = new Wiimote();

        //private bool btnPressed;


        public GameForm()
        {
            InitializeComponent();


            avatarHappy = Properties.Resources.avatar_happy;
            avatarSad = Properties.Resources.avatar_sad;
            avatarCongrats = Properties.Resources.avatar_congrats;
            //waitLoading = Properties.Resources.clock_2;


        }


        private void GameForm_Load(object sender, EventArgs e)
        {
            this.StartPosition = FormStartPosition.CenterScreen;
            this.WindowState = FormWindowState.Maximized;

            this.workspacePictureBox.SizeMode = PictureBoxSizeMode.CenterImage;

            //this.avatarPictureBox.Image = waitLoading;
            this.avatarPictureBox.SizeMode = PictureBoxSizeMode.CenterImage;

            this.wiiInstructionsPictureBox.SizeMode = PictureBoxSizeMode.CenterImage;

            this.textInstructions.Text = "Please Wait for the game to load....";

            initializeWiiMote();
            this.avatarPictureBox.Image = null;
            this.avatarPictureBox.Invalidate();
            this.wiiInstructionsPictureBox.Image = null;
            this.wiiInstructionsPictureBox.Invalidate();
            this.dragImages2.setTextInstuctions(this.textInstructions);
            this.dragImages2.setAvatarPictureBox(this.avatarPictureBox);
           

            this.dragImages2.DrawBBoxCircle();
            this.dragImages2.DrawNewRectangle();
            this.dragImages2.DrawBoxRectangle();
            this.dragImages2.DrawNewCircle();
            this.dragImages2.DrawBoxCircle();
            this.dragImages2.DrawNewPie();
            this.dragImages2.DrawBoxPie();
           

        }

        private void GameFormClosing(object sender, FormClosingEventArgs e)
        {
            wm.SetLEDs(true, false, false, true);
            wm.Disconnect();
            wm.Dispose();
            this.dragImages2.Visible = false;
        }

        private void initializeWiiMote()
        {
            wm.WiimoteChanged += new EventHandler<WiimoteChangedEventArgs>(wm_WiimoteChanged);
            try
            {
                wm.Connect();
            }
            catch (Exception)
            {
                //DO SOMETHING
                System.Windows.Forms.MessageBox.Show("Please make sure the wiiMote is connected");
                throw;
            }

            wm.SetReportType(InputReport.IRAccel, true);
            wm.SetLEDs(false, true, false, false);
        }

        private void wm_WiimoteChanged(object sender, WiimoteChangedEventArgs args)
        {
            WiimoteState wms = args.WiimoteState;

            if (wms.ButtonState.Home)
            {
                gameClose();
            }

            else
            {
                processWiiMoteState(wms);
            }

        }



        public void gameClose()
        {
            if (this.InvokeRequired)
            {
                this.Invoke((MethodInvoker)delegate()
                {
                    gameClose();
                });
            }

            else
                this.Close();
        }

        public void changeAvatarImg(String mood)
        {
            if (mood.Equals(GOOD_JOB))
            {
                this.avatarPictureBox.Image = avatarHappy;
            }
            else if (mood.Equals(WRONG))
            {
                this.avatarPictureBox.Image = avatarSad;
            }
            if (mood.Equals(FINISH))
            {
                this.avatarPictureBox.Image = avatarCongrats;
            }
        }

        public virtual void processWiiMoteState(WiimoteState wms)
        {
            //DO nothing. This method should be overrided by children
        }

      

    }
}
